//
//  PhotoPopoverView.m
//  XRmenu-HD
//
//  Created by shengchao yang on 11-12-10.
//  Copyright (c) 2011年 home user. All rights reserved.
//

#import "PhotoPopoverView.h"

@implementation PhotoPopoverView
@synthesize onePluPlu;

-(id) initWithStyle:(NSString*)stylename DishSubViewName:(NSString*)vname WithPluPlu:(PluPlu *)_onePluPlu{
    self = [super initWithStyle:stylename DishSubViewName:vname];
    self.onePluPlu=_onePluPlu;
    if (self && self.onePluPlu) {        
        if([self viewWithTag:kTagOfStartImgV]){
            UIImageView *imgview = (UIImageView*)[self viewWithTag:kTagOfStartImgV];
            
            XRmenuError* error = nil;
            UIImage *img=[UIImage imageWithContentsOfFile:[XRmenuPublic getDishPhotoImageFileName:(self.onePluPlu.ppPluPluId) Error:&error]];
            [imgview setImage:img];
        }
        if ([self viewWithTag:kTagOfStartLabel]) {//菜名
            [((UILabel *)[self viewWithTag:kTagOfStartLabel]) setText:JsonFiled(self.onePluPlu.ppNames)];
        }
        
        if ([self viewWithTag:kTagOfStartLabel+1]) {//价格
            NSString* unit=JsonFiled(self.onePluPlu.ppUnits);
            NSString* price  = [NSString stringWithFormat:@"%@%0.2f",[APPSESSION.allconfig getStringValueAtPath:@"System Config/currency"],[self.onePluPlu.ppPrice floatValue]];
            if ([self.onePluPlu.ppPrice floatValue]==0) {
                price=loc(@"时价");
            }
            [((UILabel *)[self viewWithTag:kTagOfStartLabel+1]) setText:[NSString stringWithFormat:@"%@/%@",price,unit]];
        }
        
        if ([self viewWithTag:kTagOfStartLabel+2]) {//备注
            ((UILabel *)[self viewWithTag:kTagOfStartLabel+2]).numberOfLines=12;//设置备注信息的行数
            
            //根据菜类不同，拼接出不同的备注信息
            NSString *ppRemarks=[NSString stringWithFormat:@"%@：%@\r\n%@：",loc(@"备注"), JsonFiled(self.onePluPlu.ppRemarks), loc(@"类别")];
            if ([self.onePluPlu.ppType isEqualToString:@"0"]) {//食品
                ppRemarks=[NSString stringWithFormat:@"%@%@\r\n%@：%@\r\n%@：%@\r\n%@", 
                           ppRemarks,
                           loc(@"食品"),
                           loc(@"材料"),
                           JsonFiled(self.onePluPlu.rsToPluFood.pfMeterials),
                           loc(@"味型"),//原为烧法  重庆项目要求改为味型
                           JsonFiled(self.onePluPlu.rsToPluFood.pfCooks),
                           JsonFiled(self.onePluPlu.rsToPluFood.pfRemarks)];
            }
            else if([self.onePluPlu.ppType isEqualToString: @"1"]){//香烟
                ppRemarks=[NSString stringWithFormat:@"%@%@\r\n%@：%@\r\nCO:%d\r\nNicotine:%d\r\nTar:%d\r\n%@", 
                           ppRemarks,
                           loc(@"香烟"),
                           loc(@"包装"),
                           JsonFiled(self.onePluPlu.rsToPluCigarete.pcPackages),
                           [self.onePluPlu.rsToPluCigarete.pcCO intValue],
                           [self.onePluPlu.rsToPluCigarete.pcNicotine intValue],
                           [self.onePluPlu.rsToPluCigarete.pcTar intValue],
                           JsonFiled(self.onePluPlu.rsToPluCigarete.pcRemarks)];
            }
            else if([self.onePluPlu.ppType isEqualToString:@"2"]){//酒水
                ppRemarks=[NSString stringWithFormat:@"%@%@\r\n%@：%@\r\n%@：%@\r\n%@：%d\r\n%@：%@\r\n%@", 
                           ppRemarks,
                           loc(@"酒水"),
                           loc(@"年份"),
                           self.onePluPlu.rsToPluDrink.pdYear,
                           loc(@"产地"),
                           JsonFiled(self.onePluPlu.rsToPluDrink.pdPlaces),
                           loc(@"容量"),
                           [self.onePluPlu.rsToPluDrink.pdCapacity intValue], 
                           loc(@"酒精度"),
                           self.onePluPlu.rsToPluDrink.pdPrecision,
                           JsonFiled(self.onePluPlu.rsToPluDrink.pdRemarks)];
            }
            
            [((UILabel *)[self viewWithTag:kTagOfStartLabel+2]) setText:ppRemarks];
            
            if ([self viewWithTag:kTagOfStartImgV + 1]) {
                if (self.onePluPlu.rsToDishOrder != nil) {
                    [self viewWithTag:kTagOfStartImgV + 1].hidden=NO;
                }else{
                    [self viewWithTag:kTagOfStartImgV + 1].hidden=YES;
                }
            }
        }        
    }
    
    return self;
}
#pragma mark 重载button处理方法
-(void) buttonClicked:(id)sender
{
	NSInteger bid = [sender tag];
	if (bid == (kTagOfStartBT + 0)) {
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        //1.插入点单数据
        XRmenuError *outErr=nil;
        [XRmenuCoreData insertNewDishOrder:self.onePluPlu Error:&outErr];
        //2.更新我的点单数量
        postN(kMessage_MainView_MyOrderNumberChanged);
        //3.更新显示模式界面里的选中状态
        postN(kMessage_DishplayModeView_UpdateData);
        //4.设置已点标志
        [self viewWithTag:kTagOfStartImgV + 1].hidden=NO;
	}
    else if (bid == (kTagOfStartBT + 1)) {      
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
        postN(kMessage_RootView_PhotoPopoverViewDisappear);
        postN(kMessage_MyOrderView_ReloadTableViewData);
        [APPSESSION updateCurrentPluIndex:APPSESSION.currentPluIndex];//还原当前选中的菜品对象
	}
}
@end
